//
//  MTZVisitorView.swift
//  weibomtz
//
//  Created by 毛铁柱 on 16/10/19.
//  Copyright © 2016年 mtz. All rights reserved.
//

import UIKit

///// 访客登录视图协议
//protocol MTZVisitorViewDelegate: NSObjectProtocol{
//    func registerButtonClick()
//    func loginButtonClick()
//}

class MTZVisitorView: UIView {
    
//    weak var delegate:MTZVisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    


    
    // 提供一个方法供外界去设置当前访客视图里面的内容
    
    /// 设置当前访客视图的内容
    ///
    /// - parameter imgNamed: <#imgNamed description#>
    /// - parameter message:  <#message description#>
    
    func setInfo(imgNamed:String?,message:String?){
        if let name = imgNamed {
            iconView.image = UIImage(named: name)
        }else{
            animation()
        
        }
        messageLabel.text = message
        
    }
    
    //圆圈执行的动画
    private func animation(){
        //要执行动画就让其显示出来
        circleView.hidden = false
        // 初始动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 结束值
        anim.toValue = 2 * M_PI
        // 执行一次的时间
        anim.duration = 20
        // 重复次数
        anim.repeatCount = MAXFLOAT
        
        //在切换界面或退出界面再进入，动画默认会被释放
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    
    }
//    
//    @objc private func registerButtonClick(){
//    
//       delegate?.registerButtonClick()
//    }
//    
//    @objc private func loginButtonClick(){
//        
//       delegate?.loginButtonClick()
//    
//    }
    
    // 懒加载控件
    
    //大图标
    private lazy var iconView:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_house"))
    //圆圈
    private lazy var circleView:UIImageView = {
        let imageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        imageView.hidden = true
        return imageView
    
    }()
    // 文字的label
    private lazy var messageLabel:UILabel = {
       let label = UILabel(textColor: UIColor.grayColor(), fontSize: 14)
       label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //设置换行，
        label.numberOfLines = 0
        //文字居中
        label.textAlignment = .Center
        return label
        
    }()
    // 注册按钮
     lazy var registerButton:UIButton = {
       let button = UIButton()
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitle("注册", forState: .Normal)
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named:"common_button_white"), forState: .Normal)
//        button.addTarget(self, action: "registerButtonClick", forControlEvents: .TouchUpInside)
        return button
    }()
    //登陆按钮
     lazy var loginButton:UIButton = {
        let button = UIButton()
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitle("登录", forState: .Normal)
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named:"common_button_white"), forState: .Normal)
//        button.addTarget(self, action: "loginButtonClick", forControlEvents: .TouchUpInside)
        return button
    }()
    
    // 蒙板
    private lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
}
extension MTZVisitorView{
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        // 添加子控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        // 设置约束
        // 使用autolayout前先关闭autoresizing
        // 大图标
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        // 大圆圈
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        // 消息label
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute:.NotAnAttribute, multiplier: 1, constant: 224))
        // 注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        //约束宽度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        // 登陆按钮
         loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        //约束宽度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        //蒙板
        // vfl: 可视化的格式化语言,意义在于替换系统的添加约束的 api
        // H: 水平方向约束
        // V: 垂直方向约束
        // |: 边界
        // metrics: 约束的数值字典
        // views: 约束的控件字典

        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-(margin)-[regisiterButton]", options: [], metrics: ["margin" : -35], views: ["view" : maskIconView,"regisiterButton":registerButton]))

    }



}
